package com.ljl.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljl.springboot.entity.User;

import com.ljl.springboot.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/save")
    public boolean save(@RequestBody User user){
        return userService.saveUser(user);
    }

    @GetMapping("/")
    public List<User> findall(){
        return userService.list();
    }
    @DeleteMapping("/{id}")
    public Boolean delete(@PathVariable Integer id){
        return userService.removeById(id);
    }
    @PostMapping("/del/batch")
    public Boolean deleteBatch(@RequestBody List<Integer> ids){
        return userService.removeByIds(ids);
    }
//分页查询 mplus方法
    @GetMapping("/page")
    public IPage<User> findpage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam(defaultValue = "") String username,
                                @RequestParam(defaultValue = "") String sex,
                                @RequestParam(defaultValue = "") String address
                                ){
        IPage<User> page=new Page<>(pageNum,pageSize);
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        if (!"".equals(username)){
            queryWrapper.like("username",username);
        }
        if (!"".equals(sex)){
            queryWrapper.like("sex",sex);
        }
        if (!"".equals(address)){
            queryWrapper.like("address",address);
        }
        queryWrapper.orderByDesc("id");

        return userService.page(page, queryWrapper);
    }

    //分页查询
    //接口路径
    //@RequestParam接受 ？pageNum=1&pageSize=10
//    @GetMapping("/page")
//    public Map<String, Object> findpage(@RequestParam Integer pageNum,
//                                        @RequestParam Integer pageSize,
//                                        @RequestParam String username){
//        pageNum=(pageNum-1)*pageSize;
//        username="%"+username+"%";
//        List<User> data=userMapper.selectPage(pageNum,pageSize,username);
//        Integer total=userMapper.selectTotal(username);
//        Map<String, Object> res = new HashMap<>();
//        res.put("data",data);
//        res.put("total",total);
//        return res;
//    }
}